// 调整图片方向
export default {
  start (file) {
    let _this = this
    let fileType = file.type
    let p = new Promise((resolve, reject) => {
      let base64Data = ''
      let img = new Image()
      img.onload = () => {
        let Orientation = ''
        window.EXIF.getData(file, function () {
          window.EXIF.getAllTags(this)
          Orientation = window.EXIF.getTag(this, 'Orientation')
          //  如果方向角不为1，都需要进行旋转
          if (Orientation === 6 || Orientation === 8 || Orientation === 3) {
            let canvas = document.createElement('canvas')
            switch (Orientation) {
              case 6:// 需要顺时针（向左）90度旋转
                _this.rotateImg(img, 'left', canvas)
                break
              case 8:// 需要逆时针（向右）90度旋转
                _this.rotateImg(img, 'right', canvas)
                break
              case 3:// 需要180度旋转
                _this.rotateImg(img, 'right', canvas)// 转两次
                _this.rotateImg(img, 'right', canvas)
                break
            }
            base64Data = canvas.toDataURL(fileType, 0.97)
            resolve({file: _this.convertBase64UrlToBlob(base64Data), base64Data})
          } else {
            resolve({file, base64Data})
          }
        })
      }
      // 把图片读成base64数据
      let ready = new FileReader()
      /* 开始读取指定的Blob对象或File对象中的内容. 当读取操作完成时,readyState属性的值会成为DONE,如果设置了onloadend事件处理程序,则调用之.同时,result属性中将包含一个data: URL格式的字符串以表示所读取文件的内容. */
      ready.onload = (e) => {
        base64Data = e.target.result
        img.src = e.target.result
      }
      ready.readAsDataURL(file)
    })
    return p
  },
  rotateImg (img, direction, canvas) {
    // 最小与最大旋转方向，图片旋转4次后回到原方向
    let minStep = 0
    let maxStep = 3
    if (img == null) return
    let height = img.height
    let width = img.width
    let step = 2
    if (step == null) {
      step = minStep
    }
    if (direction === 'right') {
      step++
      // 旋转到原位置，即超过最大值
      step > maxStep && (step = minStep)
    } else {
      step--
      step < minStep && (step = maxStep)
    }
    // 旋转角度以弧度值为参数
    let degree = step * 90 * Math.PI / 180
    let ctx = canvas.getContext('2d')
    switch (step) {
      case 0:
        canvas.width = width
        canvas.height = height
        ctx.drawImage(img, 0, 0)
        break
      case 1:
        canvas.width = height
        canvas.height = width
        ctx.rotate(degree)
        ctx.drawImage(img, 0, -height)
        break
      case 2:
        canvas.width = width
        canvas.height = height
        ctx.rotate(degree)
        ctx.drawImage(img, -width, -height)
        break
      case 3:
        canvas.width = height
        canvas.height = width
        ctx.rotate(degree)
        ctx.drawImage(img, -width, 0)
        break
    }
  },
  // base64转blob
  convertBase64UrlToBlob (urlData) {
    let arr = urlData.split(',')
    let mime = arr[0].match(/:(.*?);/)[1]
    let bstr = atob(arr[1])
    let n = bstr.length
    let u8arr = new Uint8Array(n)
    while (n--) {
      u8arr[n] = bstr.charCodeAt(n)
    }
    let blob = new Blob([u8arr], {type: mime})
    return blob
  }
}
